package se.haaga.jsupervision.gui;

import java.awt.Component;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import se.haaga.jsupervision.gui.linux.LinuxMonitorTab;
import se.haaga.jsupervision.listeners.JUbuntuActionListener;
import se.haaga.jsupervision.listeners.JUbuntuWindowListener;
import se.haaga.jsupervision.monitor.linux.LinuxMonitors;
import se.haaga.jsupervision.util.ActionConstants;
import se.haaga.jsupervision.util.Translator;
import se.haaga.jsupervision.util.UserVariables;

public class MainWindow extends JFrame {

	private static final long serialVersionUID = -4783369275792756226L;
	
	private JUbuntuWindowListener windowListener = null;
	
	private JUbuntuActionListener actionListener = null;
	
	private JTabbedPane jTabbedPane = null;

	public MainWindow() {
		initListeners();
		initFrame();
		initPanels();
		initMenu();
	}

	private void initPanels() {
		jTabbedPane = new JTabbedPane(JTabbedPane.TOP);
		JLabel jlWelcome = new JLabel(Translator.TAB_WELCOME.getTranslation());
		jTabbedPane.add(Translator.TAB_WELCOME.getTranslation(), jlWelcome);
		getContentPane().add(jTabbedPane);
	}

	private void initMenu() {
		JMenuBar jMenuBar = new JMenuBar();
		
		// The file menu
		JMenu jmFile = new JMenu(Translator.MENU_FILE.getTranslation());
		JMenuItem jmiExit = new JMenuItem(Translator.MENU_FILE_EXIT.getTranslation());
		jmiExit.addActionListener(actionListener);
		jmiExit.setActionCommand(ActionConstants.EXIT.toString());
		jmFile.add(jmiExit);
		jMenuBar.add(jmFile);
		
		// Connections menu
		JMenu jmConnections = new JMenu(Translator.MENU_MONITORS.getTranslation());
		JMenu jmLinux = new JMenu(Translator.MENU_MONITORS_LINUX.getTranslation());
		LinuxMonitors connections = LinuxMonitors.getInstance();
		List<String> connectionNames = connections.getLinuxMonitorsNames();
		for (String connectionName : connectionNames) {
			JMenuItem jmiConnection = new JMenuItem(connectionName);
			jmiConnection.addActionListener(actionListener);
			jmiConnection.setActionCommand(ActionConstants.ADD_LINUX_CONNECTION_TAB.toString());
			jmLinux.add(jmiConnection);
		}
		jmLinux.addSeparator();
		JMenuItem jmiNewConnection = new JMenuItem(Translator.MENU_MONITORS_LINUX_NEW_CONNECTION.getTranslation());
		jmiNewConnection.addActionListener(actionListener);
		jmiNewConnection.setActionCommand(ActionConstants.ADD_NEW_CONNECTION.toString());
		jmLinux.add(jmiNewConnection);		
		jmConnections.add(jmLinux);
		jMenuBar.add(jmConnections);
		
		// Window menu
		JMenu jmWindows = new JMenu(Translator.MENU_WINDOWS.getTranslation());
		JMenuItem jmiCloseTab = new JMenuItem(Translator.MENU_WINDOWS_CLOSETAB.getTranslation());
		jmiCloseTab.addActionListener(actionListener);
		jmiCloseTab.setActionCommand(ActionConstants.CLOSE_TAB.toString());
		jmWindows.add(jmiCloseTab);		
		jMenuBar.add(jmWindows);
		
		// Help menu
		JMenu jmHelp = new JMenu(Translator.MENU_HELP.getTranslation());
		JMenuItem jmiAbout = new JMenuItem(Translator.MENU_HELP_ABOUT.getTranslation());
		jmiAbout.addActionListener(actionListener);
		jmiAbout.setActionCommand(ActionConstants.ABOUT.toString());
		jmHelp.add(jmiAbout);		
		jMenuBar.add(jmHelp);
		
		setJMenuBar(jMenuBar);
	}

	private void initListeners() {
		windowListener = new JUbuntuWindowListener();
		actionListener = new JUbuntuActionListener(this);
	}

	private void initFrame() {
		setLookAndFeel();
		// Create the frame
		String title = Translator.MAIN_PROGRAM_TITLE.getTranslation() + " - " + Translator.VERSION.getTranslation();
		setTitle(title);

		setSize(UserVariables.MAIN_PROGRAM_WIDTH, UserVariables.MAIN_PROGRAM_HEIGHT);
		addWindowListener(windowListener);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	
	public void addToTabPane(String name, JComponent c) {
		jTabbedPane.add(name, c);
		jTabbedPane.setSelectedComponent(c);
	}
	
	public void closeTabPane() {
		jTabbedPane.remove(jTabbedPane.getSelectedIndex());
	}

	private void setLookAndFeel() {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if (UserVariables.LOOK_AND_FEEL_NAME.equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
        } catch (Exception e) {
            System.err.println("Couldn't set the desired look and feel. " + e.getMessage());
        }
	}
	
	public boolean isTabOpen(String text) {
		int tabCount = jTabbedPane.getTabCount();
		for (int i = 0; i < tabCount; i++) {
			Component c = jTabbedPane.getComponent(i);
			if (c instanceof LinuxMonitorTab) {
				LinuxMonitorTab ct = (LinuxMonitorTab) c;
				if (ct.getConnectionName().equals(text)) {
					jTabbedPane.setSelectedIndex(i);
					return true;
				}
			}
		}
		return false;
	}
	
	public void confirmExit() {
		int answer = JOptionPane.showConfirmDialog(this, 
				Translator.EXIT_CONFIRM_MESSAGE.getTranslation(), 
				Translator.EXIT_CONFIRM_TITLE.getTranslation(), 
				JOptionPane.YES_NO_OPTION);
		if (answer == JOptionPane.YES_OPTION) {
			closeAllMonitors();
			dispose();
			System.exit(0);
		}
	}
	
	private void closeAllMonitors() {
		LinuxMonitors linuxMonitors = LinuxMonitors.getInstance();
		linuxMonitors.closeAllConnections();
	}

}
